<?php
/**
 * Handles Product Pricing.
 * @copyright Nicolas Schudel 2008
 * @copyright Portions Copyright 2003-2006 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
 */

class Prices {
	private $db;
	private $cf;
	private $currencies;
	private $prices;
	
	function __construct() {
		$this->db = ServiceLocator::getService('ConnectionFactory');
		$this->cf = ServiceLocator::getService('Configuration');
		$this->currencies = ServiceLocator::getService('Currencies');	
		$this->db->connect($this->cf->get('DB_SERVER'), $this->cf->get('DB_SERVER_USERNAME'), $this->cf->get('DB_SERVER_PASSWORD'), $this->cf->get('DB_DATABASE'), $this->cf->get('DB_USE_PCONNECT'), false);
	}
	
	private function getBasePrice($products_id) {
		$product_check = $this->db->execute("select products_price, products_priced_by_attribute from " . $this->cf->get('TABLE_PRODUCTS') . " where products_id = '" . (int)$products_id . "'");
		$products_price = $product_check->fields['products_price'];
		$product_att_query = $this->db->execute("select options_id, price_prefix, options_values_price, attributes_display_only, attributes_price_base_included from " . $this->cf->get('TABLE_PRODUCTS_ATTRIBUTES') . " where products_id = '" . (int)$products_id . "' and attributes_display_only != '1' and attributes_price_base_included='1'". " order by options_id, price_prefix, options_values_price");
		$the_options_id= 'x';
		$the_base_price= 0;
		if ($product_check->fields['products_priced_by_attribute'] == '1' and $product_att_query->RecordCount() >= 1) {
			while (!$product_att_query->EOF) {
				if ( $the_options_id != $product_att_query->fields['options_id']) {
				$the_options_id = $product_att_query->fields['options_id'];
				$the_base_price += $product_att_query->fields['options_values_price'];
				}
				$product_att_query->MoveNext();
			}
			
			$the_base_price = $products_price + $the_base_price;
		} else {
			$the_base_price = $products_price;
		}
		return $the_base_price;
	}

	private function getSpecialPrice($product_id, $specials_price_only=false) {
		$product = $this->db->execute("select products_price, products_model, products_priced_by_attribute from " . $this->cf->get('TABLE_PRODUCTS') . " where products_id = '" . (int)$product_id . "'");
		
		if ($product->RecordCount() > 0) {
			$product_price = $this->getBasePrice($product_id);
		} else {
			return false;
		}
		
		$specials = $this->db->execute("select specials_new_products_price from " . $this->cf->get('TABLE_SPECIALS') . " where products_id = '" . (int)$product_id . "' and status='1'");
		if ($specials->RecordCount() > 0) {
			$special_price = $specials->fields['specials_new_products_price'];
		} else {
			$special_price = false;
		}
		
		if(substr($product->fields['products_model'], 0, 4) == 'GIFT') {
			if (isset($special_price)) {
				return $special_price;
			} else {
				return false;
		}
		}
		if ($specials_price_only==true) {
			if (isset($special_price)) {
				return $special_price;
			} else {
				return false;
		}
		} else {
			$product_to_categories = $this->db->execute("select master_categories_id from " . $this->cf->get('TABLE_PRODUCTS') . " where products_id = '" . $product_id . "'");
			$category = $product_to_categories->fields['master_categories_id'];
			
			$sale = $this->db->execute("select sale_specials_condition, sale_deduction_value, sale_deduction_type from " . $this->cf->get('TABLE_SALEMAKER_SALES') . " where sale_categories_all like '%," . $category . ",%' and sale_status = '1' and (sale_date_start <= now() or sale_date_start = '0001-01-01') and (sale_date_end >= now() or sale_date_end = '0001-01-01') and (sale_pricerange_from <= '" . $product_price . "' or sale_pricerange_from = '0') and (sale_pricerange_to >= '" . $product_price . "' or sale_pricerange_to = '0')");
			if ($sale->RecordCount() < 1) return $special_price;
			
			if (!$special_price) {
				$tmp_special_price = $product_price;
			} else {
				$tmp_special_price = $special_price;
			}
			switch ($sale->fields['sale_deduction_type']) {
				case 0:
					$sale_product_price = $product_price - $sale->fields['sale_deduction_value'];
					$sale_special_price = $tmp_special_price - $sale->fields['sale_deduction_value'];
				break;
				case 1:
					$sale_product_price = $product_price - (($product_price * $sale->fields['sale_deduction_value']) / 100);
					$sale_special_price = $tmp_special_price - (($tmp_special_price * $sale->fields['sale_deduction_value']) / 100);
				break;
				case 2:
					$sale_product_price = $sale->fields['sale_deduction_value'];
					$sale_special_price = $sale->fields['sale_deduction_value'];
				break;
				default:
				return $special_price;
			}
			
			if ($sale_product_price < 0) $sale_product_price = 0;
			
			if ($sale_special_price < 0) $sale_special_price = 0;
			
			if (!$special_price) {
				return number_format($sale_product_price, 4, '.', '');
			} else {
				switch($sale->fields['sale_specials_condition']){
					case 0:
						return number_format($sale_product_price, 4, '.', '');
					break;
					case 1:
						return number_format($special_price, 4, '.', '');
					break;
					case 2:
						return number_format($sale_special_price, 4, '.', '');
					break;
					default:
						return number_format($special_price, 4, '.', '');
				}
			}
		}
	}
	
	public function getPrice($products_id) {
		$product_check = $this->db->execute("select products_tax_class_id, products_price, products_priced_by_attribute, product_is_free, product_is_call from " . $this->cf->get('TABLE_PRODUCTS') . " where products_id = '" . (int)$products_id . "'" . " limit 1");
		
		$show_display_price = '';
		$display_normal_price = $this->getBasePrice($products_id);
		$display_special_price = $this->getSpecialPrice($products_id, true);
		$display_sale_price = $this->getSpecialPrice($products_id, false);
		
		$products_actual_price = $display_normal_price;
		
		if ($display_special_price) $products_actual_price = $display_special_price;
		if ($display_sale_price) $products_actual_price = $display_sale_price;
		if ($product_check->fields['product_is_free'] == '1') $products_actual_price = 0;
		
		return $products_actual_price;
	}
}
?>
